﻿using Engine;
using Game;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System;
using Game.NetWork.Packages;
using Game.NetWork;
using System.Text;
using System.Security.Cryptography;
using LiteNetLib;
using TemplatesDatabase;
using Engine.Graphics;
using GameEntitySystem;

namespace Game.Server.Plugins
{
    /// <summary>
    /// 插件指令
    /// </summary>
    public class CmdBlockFirm : AbstractProcessCmd
    {
        public override string Cmd => "blockFirm"; //指令名

		public override string Introduce => "/" + Title + "    -查看禁用方块修改的命令帮助"; //指令说明
		//public override string Introduce => Format + "  -更改方块的可修改属性\n   对所有方块生效，默认都为False。以下为支持的属性名\n" +
	 //"   IsPlaceable-是否可放置\n" +
	 //"   IsDigable-是否可挖掘\n" +
	 //"   IsDropable-是否产生掉落物\n" +
	 //"   ExplosionDamage-是否可被爆炸破坏\n" +
	 //"   ProjectileDamage-是否可被投掷物破坏\n" +
	 //"   FireDamage-是否可被火焰破坏\n"; //指令说明

		public override int AuthLevel => 100; //指令权限等级

        public override DisplayType Display => DisplayType.Authority; //指令是否可查看

        public const string Title = "blockFirm";

        private void SendHelpMessage()
        {
            string message = "\n" +
                "/blockFirm 属性名 值(True/False)    -更改方块的可修改属性\n" +
                "/blockFirm help    -查看支持的属性名";
            SendMessage(Title, message);
        }

        public override void Process(string username, NetNode netNode, Client client, string[] messageDatas, bool isTerminal = false)
        {
            //if (!isTerminal)
            //    return;
            m_username = username;
            m_netNode = netNode;
            m_client = client;
            m_messageDatas = messageDatas;
            m_isTerminal = isTerminal;
            ProcessCmd();
        }

        /// <summary>
        /// 指令执行
        /// </summary>
        public override void ProcessCmd()
        {
            //test txt根据空格分隔，第一个m_messageDatas[0]=test, 第二个m_messageDatas[1]=txt
            if (m_messageDatas.Length <= 1)
            {
                SendHelpMessage();
            }
			else
			{
                if (m_messageDatas[1] == "help")
                {
					string message = "插件_禁用方块修改  帮助  显示所有属性名：\n" +
			            "以下属性对所有方块生效，默认都为False\n" +
						"IsPlaceable-是否可放置\n" +
						"IsDigable-是否可挖掘\n" +
						"IsDropable-是否产生掉落物\n" +
						"ExplosionDamage-是否可被爆炸破坏\n" +
						"ProjectileDamage-是否可被投掷物破坏\n" +
						"FireDamage-是否可被火焰破坏";
					SendMessage(Title, message);
				}
				else
				{
                    string attribute = m_messageDatas[1];
                    bool flag;
					try
					{
                        flag = Convert.ToBoolean(m_messageDatas[2]);
                    }
					catch
					{
                        SendMessage(Title, "非法的属性值！请输入True或False");
                        return;
                    }
                    switch (attribute)
                    {
                        case "IsPlaceable": BlockFirmPlugin.IsPlaceable = flag; break;
                        case "IsDigable": BlockFirmPlugin.IsDigable = flag; break;
                        case "IsDropable": BlockFirmPlugin.IsDropable = flag; break;
                        case "ExplosionDamage": BlockFirmPlugin.ExplosionDamage = flag; break;
                        case "ProjectileDamage": BlockFirmPlugin.ProjectileDamage = flag; break;
                        case "FireDamage": BlockFirmPlugin.FireDamage = flag; break;
                        default:
                            SendMessage(Title, "未知的属性名，请输入/blockFirm help查看可用属性名");
                            return;
                    }
                    BlockFirmPlugin.ModifyBlockAttributes();
                    SendMessage(Title, "已完成方块属性修改:" + attribute + "-" + flag.ToString());
                }
            }
   //         string cmd = m_messageDatas[0];
   ////         if (cmd == "blockFirmHelp")
			////{
   ////             string message = "插件_禁用方块修改  帮助  显示所有属性名：\n" +
			////		"以下属性对所有方块生效" +
   ////                 "IsPlaceable-是否可放置\n" +
   ////                 "IsDigable-是否可挖掘\n" +
   ////                 "IsDropable-是否产生掉落物\n" +
   ////                 "ExplosionDamage-是否可被爆炸破坏\n" +
   ////                 "ProjectileDamage-是否可被投掷物破坏\n" +
   ////                 "FireDamage-是否可被火焰破坏";
   ////             SendMessage(Title, message);
   ////             return;
   ////         }
   ////         else
   //         if (m_messageDatas.Length < 3 || cmd != "blockFirm")
   //         {
   //             SendMessage(Title, "请按照\"" + Format + " \"格式输入指令");
   //             return;
   //         }
   //         string attribute = m_messageDatas[1];
   //         bool flag = Convert.ToBoolean(m_messageDatas[2]);
			//switch (attribute)
			//{
   //             case "IsPlaceable": BlockFirmPlugin.IsPlaceable = flag; break;
   //             case "IsDigable": BlockFirmPlugin.IsDigable = flag; break;
   //             case "IsDropable": BlockFirmPlugin.IsDropable = flag; break;
   //             case "ExplosionDamage": BlockFirmPlugin.ExplosionDamage = flag; break;
   //             case "ProjectileDamage": BlockFirmPlugin.ProjectileDamage = flag; break;
   //             case "FireDamage": BlockFirmPlugin.FireDamage = flag; break;
   //             default:
   //                 SendMessage(Title, "未知的属性名，请输入/blockFirmHelp查看可用属性名");
   //                 return;
   //         }
   //         BlockFirmPlugin.ModifyBlockAttributes();
   //         SendMessage(Title, "已完成方块属性修改:" + attribute + "-" + flag.ToString());
        }
    }

    /// <summary>
    /// 插件服务
    /// </summary>
    public class BlockFirmPlugin : ServerPlugin
    {
        public override int Version => 10000; //版本号

        public override string Name => "插件_禁用方块修改"; //插件名

        public byte FirstLevel => 0; //优先级

        /// <summary>
        /// 加载插件时执行（初始化）
        /// </summary>
        public override void Initialize()
        {
            Log.Warning("插件_禁用方块修改已完成初始化");
            IsPlaceable = false;
            IsDigable = false;
            IsDropable = false;
            ExplosionDamage = false;
            ProjectileDamage = false;
            FireDamage = false;
        }

        /// <summary>
        /// 存档加载时执行
        /// </summary>
        public override void Load()
        {
            ModifyBlockAttributes();
            Log.Warning(Name + " 已加载配置：" +
                "\nIsPlaceable-" + IsPlaceable.ToString() +
                "\nIsDigable-" + IsDigable.ToString() +
                "\nIsDropable-" + IsDropable.ToString() +
                "\nExplosionDamage-" + ExplosionDamage.ToString() +
                "\nProjectileDamage-" + ProjectileDamage.ToString() +
                "\nFireDamage-" + FireDamage.ToString());
        }

        /// <summary>
        /// 存档保存时执行
        /// </summary>
        public override void Save()
        {

        }

        public static void ModifyBlockAttributes()
		{
            foreach (ModEntity modEntity in ModsManager.ModList)
            {
                modEntity.LoadBlocksData();
            }
            for (int i = 1; i < BlocksManager.Blocks.Length; i++)
            {
                try
                {
                    Block block = BlocksManager.Blocks[i];
                    block.IsPlaceable = IsPlaceable;
                    if (!IsDigable)
                        block.DigResilience = float.PositiveInfinity;
                    if (!ExplosionDamage)
                        block.ExplosionResilience = float.PositiveInfinity;
                    if (!ProjectileDamage)
                        block.ProjectileResilience = float.PositiveInfinity;
                    if (!FireDamage)
                        block.FireDuration = 0;
                    if (!IsDropable)
					{
                        block.DefaultDropCount = 0;
                        block.DefaultExperienceCount = 0;
                    }
                }
                catch
                {
                }
            }
        }

        public static bool IsPlaceable;
        public static bool IsDigable;
        public static bool IsDropable;
        public static bool ExplosionDamage;
        public static bool ProjectileDamage;
        public static bool FireDamage;
    }
}